<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\learn\article;

use app\api\service\User as UserService;
use app\common\model\learn\article\TitleUser as TitleUserModel;
use app\api\model\learn\Clock as ClockModel;
use think\facade\Db;

/**
 * 商品评价模型
 * Class Article
 * @package app\api\model
 */
class TitleUser extends TitleUserModel {

	/**
	 * 获取文章列表
	 * @param int $categoryId
	 * @param int $limit
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getList($type = 0, int $bookId = 0, int $limit = 15): \think\Paginator {
		$userId = UserService::getCurrentLoginUserId();
		if ($type == 0) {
			// 检索查询条件
			$filter = [];
			$filter[] = ['book_id', '=', $bookId];
			// 获取列表数据
			return $this->alias('u')
				->join('learn_article_title t', 't.article_id=u.article_id')

				->where($filter)
				->where('user_id', '=', $userId)
				->where('u.status', '=', 0)
				->field('t.book_name,t.book_name2,t.content,t.article_id')
				->paginate($limit);
		}
		if ($type == 1) {
			// 检索查询条件
			$filter = [];
			$filter[] = ['book_id', '=', $bookId];
			// 获取列表数据
			return $this->alias('u')
				->join('learn_article_title t', 't.article_id=u.article_id')

				->where($filter)
				->where('user_id', '=', $userId)

				->field('t.book_name,t.book_name2,t.content,t.article_id')
				->paginate($limit);
		}
		if ($type == 2) {
			$book_name = Db::name('learn_article_category')->where('category_id', $bookId)->value('name');

			$log = $this
				->where('user_id', '=', $userId)->select()->toArray();
			$ids = array_column($log, 'article_id');

			// 获取列表数据
			return Db::name('learn_article_title')

				->where('article_id', 'not in', $ids)
				->where('book_name', '=', $book_name)
				->field('book_name,book_name2,content,article_id')
				->paginate($limit);
		}
	}
/**
 * 获取复习条文列表
 * @param int $categoryId
 * @param int $limit
 * @return \think\Paginator
 * @throws \think\db\exception\DbException
 */
	public function getReviewList(int $bookId = 0, int $limit = 15): \think\Paginator {
		$userId = UserService::getCurrentLoginUserId();

		// 检索查询条件
		$filter = [];
		$filter[] = ['book_id', '=', $bookId];
		// 获取列表数据
		return $this->alias('u')
			->join('learn_article_title t', 't.article_id=u.article_id')

			->where($filter)
			->where('user_id', '=', $userId)
			->where('u.review_num', '>', 0)

			->field('t.book_name,t.book_name2,t.content,t.article_id,u.log_id')
			->order('u.review_num desc,u.create_time desc')
			->paginate($limit);

	}

	public function handRen($data) {

		if ($this['status'] == 0) {
			//已学习+1
			Db::name('learn_article_category_user')
				->where('user_id', $this['user_id'])
				->where('book_id', $this['book_id'])
				->inc('complate_num', 1)->update();
		}

		$this->save(['status' => $data['status'], 'time' => $this['time'] + $data['time']]);

	//打卡

		$model=new ClockModel;
		$model->add($this['user_id']);
	
		return true;
	}
	public function handEasy() {
		$this->save(['is_easy' => 1]);
		Db::name('learn_article_title')->where('article_id', $this['article_id'])->inc('easy_num', 1)->update();
		return true;
	}

	public function review($num, $time) {
		$this->save(['review_num' => $num]);
		$this->save(['time' => $this['time'] + $time]);
			//打卡

		$model=new ClockModel;
		$model->add($this['user_id'],$time);
	
		return true;
	}
	public function reviewDel($ids) {
		$this->where('log_id', 'in', $ids)->update(['review_num' => 0]);

		return true;
	}
}